var oneTuWenValidate;
function oneTuWen(id,url){
	var param = "";
	if(id){
		param = "/id/"+id;
	}
	$.get(url+param,function(html){
		$("#main_content").html(html);
		
	oneTuWenValidate = $('form').validate({
							onKeyup : true,
							eachValidField : function() {
								$(this).closest('div').removeClass('error').addClass('success');
							},
							eachInvalidField : function() {
								$(this).closest('div').removeClass('success').addClass('error');
							}
						});
	});
}
var moreTuWenValidate;			
function moreTuWen(id,url){
	var param = "";
		if(id){
			param = "/id/"+id;
		}
		$.get(url+param,function(html){
			$("#main_content").html(html);
			$(".currentEditItem").click();
			
			moreTuWenValidate = $('form').validate({
							onKeyup : true,
							eachValidField : function() {
								$(this).closest('div').removeClass('error').addClass('success');
							},
							eachInvalidField : function() {
								$(this).closest('div').removeClass('success').addClass('error');
							}
						});
			
		});
}

//show 摘要
function showZhaiYao(obj){
	$(obj).hide();
	$('#zhaiyaoId').show();
}
//show原文链接
function showYuanWenlj(obj){
	$(obj).hide();
	$('#yuanwenljId').show();
}
//编辑标题
function editBiaoTi(obj){
	$("#biaotiVlaue").html(obj.value);
}
//编辑摘要
function editZhaiYao(obj){
	$(".appmsg_desc").html(obj.value);
}
function getRootPath()   
{
	var pathName = window.location.pathname.substring(1);
	var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
	return window.location.protocol + '//' + window.location.host;
}

function ajaxFileUpload(url,downLoadUrl){
	$.ajaxFileUpload({
		url:url,
		secureuri:false,
		fileElementId:"uploadify",
		dataType:"json",
		success:function(attach,status){
			$("#fmtpId").hide();
		 	$("#fengmianId").attr("src",getRootPath()+downLoadUrl+"/id/"+attach.id);
		 	$("#fengmianId").show();
		 	$("#smallImgId").attr("src",getRootPath()+downLoadUrl+"/id/"+attach.id);
		 	$("#fmPid").show();
		 	$("#articleImageId").val(attach.id);
		},
		error:function(data,status,e){
		alert(e);
		}
	});
	return false;	
}


function deletefmP(){
	$("#fmPid").hide();
	$("#fengmianId").hide();
	$("#fmtpId").show();
	$("#articleImageId").val("");
	
	$("#js_appmsg_preview .currentEditItem").parent().parent().find(".imgClass").attr("src","");
	$("#js_appmsg_preview .currentEditItem").parent().parent().find(".imgClass").hide();
	$("#js_appmsg_preview .currentEditItem").parent().parent().find("i").addClass("default");
	
	$("#js_appmsg_preview .currentEditItem").parent().find("input[name='imgId']").val("");
}

function saveArticle(url){
	if(!oneTuWenValidate.validate()){
		return ;
	}
var oEditor = CKEDITOR.instances['TextArea1'];
var data=oEditor.getData();
$("#TextArea1").val(data);
$("#tuwenMessageFrom").ajaxSubmit({
	url:url,
	type:'post',
	dataType:'json',
	success:function(json){
		if(json.success){
			$($('#aside_nav ul li a').get(0)).click();
		}
	}
});
}

function showEditor(type){
	$('.article_type').hide();
	$('#article_type').val(type);
	$('#type_'+type).show();
    var input=$('.currentEditItem').parent().find('input[name="type[]"]');
    input.val(type);
}

function deleteArticle(id,url){
	$("#deleteDialog").dialog({
		 resizable: false,
		 modal: true,
		 buttons:{
			"确定":function(){
				$(this).dialog("close");
				$.ajax({
					url:url+'/id/'+id,
					type:'get',
					dataType:'json',
					success:function(json){
						if(json.success){
							$($('#aside_nav ul li a').get(0)).click();	
						}else{
							alert("删除失败");
						}
					}
				});
			},
			"取消":function(){
				$(this).dialog("close");
			}
		}
	});
}

function firstEdit(){
		$("#js_appmsg_editor").addClass("first");	
		$("#js_appmsg_editor").attr("style","padding-top:184px");
	}
function headFirst(){
	$("#js_appmsg_editor").removeClass("first");
	$("#js_appmsg_editor").attr("style","padding-top:0px;");
}


function addSuoLueTu(){
	var itemLength = $("#js_appmsg_preview .js_appmsg_item ").length;
	if(itemLength>10){
		alert("你最多只能添加10个图文消息");
	}else{
		$("#suoluetuId .js_appmsg_item").attr("id","appmsgItem"+(itemLength+1));	
		$("#suoluetuId .js_appmsg_item").attr("data-id",(itemLength+1));
		$("#js_appmsg_preview").append($("#suoluetuId").html());
		$('.editBtn').click(editBtnClick);
	}
}

function asynFiled(){
		var name=$(this).data('field');
		var input=$('.currentEditItem').parent().find('input[name="'+name+'"]');
		input.val($(this).val());
	}

function showmoreTitle(obj){
	$("#js_appmsg_preview .currentEditItem").parent().parent().find(".titleClass").html(obj.value);
}


function deleThisItem(obj){
	var itemLength = $("#js_appmsg_preview .js_appmsg_item ").length;
	if(itemLength<=2){
		alert("无法删除，多图文至少需要2条信息");
	}else{
		$(obj).parent().parent().remove();	
		var childrenLength = $("#js_appmsg_preview").children().length;
		for(var i=0;i<childrenLength;i++){
			$("#js_appmsg_preview").children(":eq("+i+")").attr("id","appmsgItem"+(i+1));
			$("#js_appmsg_preview").children(":eq("+i+")").attr("data-id",(i+1));
		}
		$('#js_appmsg_editor').css('padding-top',"0px");
		$('.currentEditItem').removeClass('currentEditItem');
		$('#js_appmsg_preview .editBtn :eq(0)').addClass("currentEditItem");
		var inputs=$("#js_appmsg_preview .currentEditItem").parent().find('input');
		var imgId=$("#js_appmsg_preview .currentEditItem").parent().find("input[name='imgId']").val();
		if(imgId!=""){
			$("#smallImgId").attr("src","../file/download.json?fileId="+imgId);
			$("#fmPid").show();
		}else{
			$("#smallImgId").attr("src","");
			$("#fmPid").hide();
		}
		for(var i=0;i<inputs.size();i++){
			var $input=$(inputs.get(i));
			var selector='*[data-field="'+ $input.attr('name')+'"]';
			var $field=$(selector);
			$field.first().val($input.val());
		}
	
	}
}

function editThisItem(obj){
	var dataId = $(obj).parent().parent().attr("data-id");
	var paddTop = ((parseFloat(dataId)-2)*102)+184;
	$("#js_appmsg_editor").attr("style","padding-top:"+paddTop+"px;");
}


function saveMoreTuWen(url){
	if(!moreTuWenValidate.validate()){
		return ;
	}
	$("#moreTuWenFrom").ajaxSubmit({
	url:url,
	type:'post',
	dataType:'json',
	success:function(json){
		if(json.success){
			$($('#aside_nav ul li a').get(0)).click()	
		}
	}
});
}

function deleteMoreTuWen(id,url){
	$("#deleteDialog").dialog({
		 resizable: false,
		 modal: true,
		 buttons:{
			"确定":function(){
				$(this).dialog("close");
				$.ajax({
					url:url,
					type:'post',
                    data:{id:id},
					dataType:'json',
					success:function(json){
						if(json.success){
							$($('#aside_nav ul li a').get(0)).click();	
						}else{
							alert("删除失败");						
						}
					}
				});
			},
			"取消":function(){
				$(this).dialog("close");
			}
		}
	});
}